<?php
require '../model/Db.php';
$Db = Db::getInstance();
$admin_data = $Db->isadmin();
if(!$admin_data){
	$user_data = $Db->islogin();
	if($user_data['job'] == 3){
		$admin_data = $user_data;
	}
}
if(($admin_data || $user_data['job'] != 0)){
	switch ($_GET['type']) {
		case 1://区版
			$isexist = $Db->find([
				table => ['module'],
				where => "zid = {$_GET['id']}"
			]);
			if(!$isexist){
				$flag = $Db->remove([
					table => 'zone',
					flag => true,
					where => "id = {$_GET['id']}"
				]);
			}
			echo json_encode([res => $flag]);
			break; 
		
		case 2://版面
			$flag = $Db->remove([
				table => 'module',
				flag => true,
				where => "id = {$_GET['id']}"
			]);
			echo json_encode([res => $flag]);
			break;

		case 3://种类
			$isexist = $Db->find([
				table => ['module'],
				where => "kid = {$_GET['id']}"
			]);
			if(!$isexist){
				$flag = $Db->remove([
					table => 'kind',
					flag => true,
					where => "id = {$_GET['id']}"
				]);
			}
			echo json_encode([res => $flag]);
			break;

		case 4://帖子
			
			$to = $Db->find([
				table => [content => 'a',bbs_user => 'b'],
				field => 'b.id,a.body,a.title',
				where => "a.uid = b.id and a.id = {$_GET['id']}"
			])[0];
			//发信
			$Db->save([
				table => 'mail',
				map => [
					from_id => 0,
					to_id => $to['id'],
					title => '帖子涉嫌违规',
					body => "您的帖子 {$to['title']},\n内容:{$to['body']}\n经过核实涉嫌违规,管理员已将您的帖子删除,良好的网络环境需要大家一起守护。"
				]
			]);
			//扣分
			$Db->update([
				table => 'bbs_user',
				map => [
					lv => '#lv-2#'
				],
				where => "id = {$to['id']}"
			]);
			$flag = $Db->remove([
				table => 'content',
				where => "id = {$_GET['id']}"
			]);
			echo json_encode([res => $flag]);
			break;
			
		case 5: //用户
		    $flag1 = $Db->remove([
				table => 'bbs_user',
				flag => false,
				where => "id = {$_GET['id']}"
			]);
			$flag2 = $Db->remove([
				table => 'content',
				flag => true,
				where => "uid = {$_GET['id']}"
			]);
			
			$flag3 = $Db->remove([
				table => 'mail',
				flag => true,
				where => "to_id = {$_GET['id']} or from_id = {$_GET['id']}"
			]);
			
			$flag4 = $Db->remove([
				table => 'report',
				flag => true,
				where => "uid = {$_GET['id']}"
			]);
			$flag = $flag1 || $flag2 || $flag3 || $flag4;
			echo json_encode([res => $flag]);
		    break;
		        
	}
	$Db->close();
}else{
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
}